//
//  MBScrollContentView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/5/4.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBScrollContentView.h"
#import "MBScrollViewHeaderView.h"

@interface MBScrollContentView ()<UIScrollViewDelegate>
@property (nonatomic,weak)MBScrollViewHeaderView* headerView;

@end

@implementation MBScrollContentView

-(void)setFirstScrollView:(UIScrollView *)firstScrollView
{
    _firstScrollView = firstScrollView;
    firstScrollView.delegate = self;
    [self addSubview:firstScrollView];
    
    [firstScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(self.contentHeight/2);
    }];
    
}

-(void)setSecondScrollView:(UIScrollView *)secondScrollView
{
    _secondScrollView = secondScrollView;
    secondScrollView.delegate = self;
    [self addSubview:secondScrollView];
    
    [secondScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(self.contentHeight/2);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(self.contentHeight/2);
    }];
    
    MBScrollViewHeaderView* headerView = [[MBScrollViewHeaderView alloc]init];
    headerView.alpha = 0;
    self.headerView = headerView;
    [self addSubview:headerView];
    
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(secondScrollView);
    }];
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat yOffset = scrollView.contentOffset.y;
    //判断上滑动还是下滑动
    if(scrollView == self.firstScrollView){
        if(yOffset + self.contentHeight/2 > 100 + self.fistScrollViewHeight){
            self.scrollNumView = 2;
        }
    }else if(scrollView == self.secondScrollView){
        if(yOffset < - [UIView lf_sizeFromIphone6:35]){
            self.scrollNumView = 1;
        }
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.secondScrollView) {
        CGFloat yOffset = scrollView.contentOffset.y;
        if (yOffset < 0) {
            self.headerView.alpha = (-yOffset-0.5)/35;
            if(yOffset > -[UIView lf_sizeFromIphone6:35] ){
                if (yOffset > -[UIView lf_sizeFromIphone6:30]) {
                    self.headerView.title = @"下拉，返回宝贝详情";
                }else {
                    self.headerView.title = @"释放，返回宝贝详情";
                }
            }
        }
    }
}

@end
